package com.ypc.webdemo.service.impl;

import com.ypc.remotecontrolinterface.service.RemoteControl;
import com.ypc.webdemo.service.RemoteControlService;
import com.ypc.webdemo.utils.SpringBeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Map;

/**
 * @author yinpengcheng
 * @Date 2023/7/25
 */
@Service
public class RemoteControlServiceImpl implements RemoteControlService {

    @Override
    public void open() {
        Map<String, RemoteControl> beans = getStringRemoteControlBeans();
        beans.forEach((beanName, bean) -> {
            bean.open();
        });
    }

    private Map<String, RemoteControl> getStringRemoteControlBeans() {
        Map<String, RemoteControl> beans = SpringBeanUtil.getBeans(RemoteControl.class);
        Assert.notEmpty(beans, "请先安装遥控器插件");
        return beans;
    }

    @Override
    public void close() {
        Map<String, RemoteControl> beans = getStringRemoteControlBeans();
        beans.forEach((beanName, bean) -> {
            bean.close();
        });
    }
}
